﻿using NurseStation.common;
using NurseStation.DataModels;
using NurseStation.viewmodels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace NurseStation.views
{
    /// <summary>
    /// ColSetting.xaml 的交互逻辑
    /// </summary>
    public partial class ColSetting : Window
    {
        private readonly ColSettingViewModel model;
        public ColSetting(ColSettingViewModel viewModel)
        {
            InitializeComponent();
            this.DataContext = viewModel;
            this.model = viewModel;
            viewModel.SetWin(this);

            dropGrid.PreviewMouseLeftButtonDown += DropGrid_PreviewMouseLeftButtonDown;
            dropGrid.Drop += DropGrid_Drop;
            dropGrid.DragOver += DropGrid_DragOver;
            dropGrid.DragLeave += DropGrid_DragLeave;
        }
        private int prevRowIndex = -1;
        #region dropGrid下拉选项拖动排序事件
        /// <summary>
        /// 拖拽完成事件  2022-03-15
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropGrid_DragLeave(object sender, DragEventArgs e)
        {
            if (dropPopup != null)
            {
                dropPopup.IsOpen = false;
            }
        }
        /// <summary>
        /// 拖拽中事件  2022-03-15
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropGrid_DragOver(object sender, DragEventArgs e)
        {
            int index = UIHelpers.GetDataGridItemCurrentRowIndex(e.GetPosition, dropGrid);

            for (int i = 0; i < dropGrid.Items.Count; i++)
            {
                DataGridRow r = dropGrid.ItemContainerGenerator.ContainerFromIndex(i) as DataGridRow;
                if (r != null)
                {
                    if (i != index)
                    {
                        r.BorderBrush = new SolidColorBrush(Colors.Transparent);
                        r.BorderThickness = new Thickness(0, 0, 0, 0);
                    }
                    else
                    {
                        r.BorderBrush = new SolidColorBrush(Color.FromRgb(32, 164, 230));
                        Thickness th = new Thickness(0, 0, 0, 0);

                        if (index > prevRowIndex) th = new Thickness(0, 0, 0, 1);
                        else if (index < prevRowIndex) th = new Thickness(0, 1, 0, 0);
                        r.BorderThickness = th;
                    }
                }
            }

            if (dropPopup != null)
            {
                if (!dropPopup.IsOpen)
                {
                    dropPopup.IsOpen = true;
                }
                Size popupSize = new Size(dropPopup.ActualWidth + 10, dropPopup.ActualHeight + 10);
                Point p = e.GetPosition(this);
                p.X += 10;
                p.Y += 10;
                dropPopup.PlacementRectangle = new Rect(p, popupSize);
            }
        }
        /// <summary>
        /// 拖拽后事件  2022-03-15
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropGrid_Drop(object sender, DragEventArgs e)
        {
            if (dropPopup != null)
            {
                dropPopup.IsOpen = false;
            }
            int index = UIHelpers.GetDataGridItemCurrentRowIndex(e.GetPosition, dropGrid);

            if (index < 0 || index == prevRowIndex) return;
            (dropGrid.ItemContainerGenerator.ContainerFromIndex(index) as DataGridRow).BorderThickness = new Thickness(0, 0, 0, 0);

            if (index >= dropGrid.Items.Count - 1)
            {
                index = dropGrid.Items.Count - 1;
            }
            if (model.CurrDropItem != null)
            {
                model.ChangeDroplistSequence(prevRowIndex, index);
                prevRowIndex = -1;
            }
        }
        /// <summary>
        /// 下拉选项grid行按下事件  2022-03-15
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DropGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.OriginalSource is FrameworkElement fe)
            {
                if (fe.TemplatedParent != null)
                {
                    if (fe.TemplatedParent is ScrollBar || fe.TemplatedParent is Thumb)
                    {
                        return;
                    }
                }
            }
            prevRowIndex = UIHelpers.GetDataGridItemCurrentRowIndex(e.GetPosition, dropGrid);

            if (prevRowIndex < 0) return;
            dropGrid.SelectedIndex = prevRowIndex;

            var selectedEmp = dropGrid.Items[prevRowIndex] as ColShow;

            if (selectedEmp == null) return;

            model.CurrDropItem = selectedEmp;
            DragDropEffects dragdropeffects = DragDropEffects.Move;

            if (DragDrop.DoDragDrop(dropGrid, selectedEmp, dragdropeffects) != DragDropEffects.None)
            {
                dropGrid.SelectedItem = selectedEmp;
            }
        }
        #endregion
    }
}
